<template>
    <div class="phone-root">
        <div class="input">
            <input type="text" placeholder="国家和地区">
            <span>中国&ensp;<span class="fa fa-chevron-right"></span></span>
        </div>
        <div class="input">
            <div class="num">+86</div>    
            <input 
                type="text" 
                style="width:54vw" 
                placeholder="请填写手机号码"
                v-model="tel"
            >
        </div>
        <div class="input">
            <input 
                type="text" 
                style="width:28vw" 
                placeholder="验证码"
                v-model="captcha"
            >
        </div>
        <img 
            src="/user/captcha" 
            id="cap" 
            @touchstart="changeCaptcha"
        >
        <div class="input">
            <input 
                type="text" 
                placeholder="输入登录密码，至少6位"
                v-model="psw"
            >
        </div>
        
        <button @touchstart="registClick">同意协议并注册</button>
        <a>面包旅行用户隐私协议</a>

        <div class="hasaccount" @touchstart="goLogin">
            已有账号登录
        </div>

        <Alert v-show="isAlert">{{alertContent}}</Alert>
    </div>
</template>

<script>
import {mapState} from 'vuex';
export default {
    components:{
        
    },
    computed:{
        ...mapState([
            "isAlert"
        ])
    },
    data:function(){
        return {
            tel:'',
            captcha:'',
            psw:'',
            alertContent:'',
        }
    },
    created:function(){
        this.$store.commit("setIsLoading",false);
    },
    methods:{
        registClick:function(){
            if(this.tel==''){
                this.alertContent = "手机号不能为空";
                this.$store.commit("setIsAlert",true);
                setTimeout(()=> {
                    this.$store.commit("setIsAlert",false);
                }, 1800);
                return;
            }
            if(this.psw==''){
                this.alertContent = "密码不能为空";
                this.$store.commit("setIsAlert",true);
                setTimeout(()=> {
                    this.$store.commit("setIsAlert",false);
                }, 1800);
                return;
            }
            var params = {
                tel:this.tel,
                captcha:this.captcha,
                psw:this.psw
            }
            this.$store.dispatch("requestRegist",params)
            .then(res=>{
                if(res.data.err==0){
                    this.alertContent = res.data.msg;
                    this.$store.commit("setIsAlert",true);
                    setTimeout(()=> {
                        this.$store.commit("setIsAlert",false);
                        location.href = "#/hunter/login";
                    }, 1800);
                }
                if(res.data.err==1){
                    this.alertContent = res.data.msg;
                    this.$store.commit("setIsAlert",true);
                    setTimeout(()=> {
                        this.$store.commit("setIsAlert",false);
                    }, 1800);
                }
                if(res.data.err==2||res.data.err==3){
                    this.alertContent = res.data.msg;
                    this.$store.commit("setIsAlert",true);
                    setTimeout(()=> {
                        this.$store.commit("setIsAlert",false);
                    }, 1800);
                }
                this.changeCaptcha();    
            });
        },
        changeCaptcha:function(){
            var cap = document.getElementById("cap");
            cap.setAttribute("src", "");
            cap.setAttribute("src", "/user/captcha?"+Math.random());;
        },
        goLogin:function(){
            location.href = "#/hunter/login";
        }
    }
}
</script>

<style scoped>
.phone-root{
    width: 100vw;
    height: calc(100vh - 57px);
    overflow: scroll;
    background-color: #4abdcc;
    padding: 3.5vw 7vw;
    color: #fff;
}
#cap{
    margin-left: 10vw;
    width: 36vw;
    height: 6vh;
    position: relative;
    top: 5vw;
}
.nopsw{
    font-size: 0.81rem;
    opacity: .8;
    text-align: right;
    margin-bottom: 4vw;
    overflow: scroll;
}
.input{
    width: 78vw;
    padding-left: 3vw;
    margin: 0 auto;
    height: 10vw;
    margin-top: 5vw;
    border-bottom: 1px solid #fff;
}
.input input{
    width: 58vw;
    border: none;
    background-color: transparent;
    color: #fff;
    padding-left: 1vw;
    font-size: 0.98rem;
}
.num{
    display: inline-block;
    padding-right: 4vw;
    border-right: 1px solid #fff;
}
.input:nth-of-type(1){
    margin-top: 20vw;
}
.input:nth-of-type(3){
    display: inline-block;
    width: 30vw;
    margin-left: 4vw;
}
button{
    display: block;
    border: none;
    background-color: transparent;
    border: 1px solid #fff;
    width: 64vw;
    margin: 0 auto;
    height: 7.5vh;
    margin-top: 11vh;
    border-radius: 33px;
    color: #fff;
    font-size: 1.1rem;
}
a{
    display: block;
    width:  60vw;
    margin: 0 auto;
    text-align: center;
    color: #fff;
    font-size: 0.7rem;
    opacity: .6;
    margin-top: 4vh;
    margin-bottom: 4vh;
    text-decoration: underline;
}
.hasaccount{
    position: fixed;
    left: 0;
    bottom: 0;
    text-align: center;
    width: 100vw;
    height: 57px;
    line-height: 57px;
    border-top: 1px solid #ddd;
    background-color: #4abdcc;
}
</style>